import 'package:abc/controller/point_record_controller.dart';
import 'package:abc/model/point_record.dart';
import 'package:abc/view/CustomAppBar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class PointRecordPage extends GetView<PointRecordController> {
  const PointRecordPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
     if (!Get.isRegistered<PointRecordController>()) {
      Get.lazyPut(() => PointRecordController());
    }
    return Scaffold(
      backgroundColor: const Color(0xFF0E0F26), // 深色背景
      appBar: CustomAppBar(
        title: "积分记录",
        onBack: () {
          Get.back();
        },
      ),
      body: _buildSmartRefresher(),
    );
  }
  
  Widget _buildSmartRefresher() {
    return SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: WaterDropHeader(
          complete: Text("刷新成功", style: TextStyle(color: Colors.white)),
          idleIcon: Text("下拉刷新", style: TextStyle(color: Colors.white)),
          refresh: Text("正在刷新...", style: TextStyle(color: Colors.white)),
          failed: Text("刷新失败", style: TextStyle(color: Colors.white)),
        ),
        footer: ClassicFooter(
          idleText: "上拉加载更多",
          loadingText: "正在加载更多...",
          noDataText: "已加载全部记录",
          failedText: "加载失败，请点击重试",
          canLoadingText: "松手加载更多",
          textStyle: TextStyle(color: Colors.white),
        ),
        controller: controller.refreshController,
        onRefresh: controller.onRefresh,
        onLoading: controller.onLoading,
        child: ListView.builder(
          padding: EdgeInsets.only(top: 10),
          itemBuilder: (c, i) => _buildRecordItem(controller.items[i], i, controller.items.length),
          itemCount: controller.items.length,
        ),
      );
  }
  
  // 列表项组件
  Widget _buildRecordItem(PointRecord record, int index, int total) {
    // 根据位置确定圆角
    BorderRadius borderRadius = BorderRadius.zero;
    if (total == 1) {
      borderRadius = BorderRadius.circular(10);
    } else if (index == 0) {
      borderRadius = const BorderRadius.vertical(top: Radius.circular(10));
    } else if (index == total - 1) {
      borderRadius = const BorderRadius.vertical(bottom: Radius.circular(10));
    }
    
    return Container(
      padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            color: const Color(0x33959595), // 卡片背景色
            borderRadius: borderRadius,
          ),
        child:  Row(
         mainAxisAlignment: MainAxisAlignment.spaceBetween,
         children: [
           // 左边的Row，包含Column显示原因和时间
           Column(
             crossAxisAlignment: CrossAxisAlignment.start,
             children: [
               Text(
                 record.reason,
                 style: const TextStyle(
                   color: Colors.white,
                   fontSize: 15,
                   fontWeight: FontWeight.w400,
                 ),
               ),
               const SizedBox(height: 11),
               Text(
                 record.time,
                 style: TextStyle(
                   color: Colors.white60,
                   fontSize: 12,
                   fontWeight: FontWeight.w400,
                 ),
               ),
             ],
           ),
           // 右边的Row，显示积分数量
           Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                '${record.amount > 0 ? '+' : ''}${record.amount}',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
              Text(
                '积分',
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
         ],
      ),).paddingSymmetric(horizontal: 16);
  }
}